package edu.cn.nlsde.tmfst.crawler.processor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.cn.nlsde.Utils.Utils;
import edu.cn.nlsde.tmfst.model.Comment;
import edu.cn.nlsde.tmfst.model.Photo;
import edu.cn.nlsde.tmfst.model.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.File;
import java.io.FileWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by dell on 2016/5/6.
 */
public class crawler_JD implements PageProcessor {
    boolean flag = true;
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static Logger mylog = LoggerFactory.getLogger(crawler_JD.class.getName());
    public String product_id = "";
    public int this_num = 0;
    private Site site = Site.me()
            .setSleepTime(10)
            .addHeader("Referer", "https://item.jd.com/4006282.html")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8;charset=utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
            .setCycleRetryTimes(2)
            .setRetrySleepTime(100)
            .setTimeOut(1000);

    //    static boolean isToday(String date) throws ParseException {
//        Date dt = sdf.parse(date);
//        Calendar dtc = Calendar.getInstance();
//        dtc.setTime(dt);
//        Calendar today = Calendar.getInstance();
//        today.setTime(new Date());
//        return today.get(Calendar.YEAR) == dtc.get(Calendar.YEAR)
//                && today.get(Calendar.MONTH) == dtc.get(Calendar.MONTH)
//                && today.get(Calendar.DAY_OF_MONTH) == dtc.get(Calendar.DAY_OF_MONTH);
//
//
//    }
    public crawler_JD(String product_id) {
        this.product_id = product_id;
    }

    @Override
    public void process(Page page) {
        if (flag) {
            Product pd = new Product();
            pd.setPlatform(2);
//            pd.setProduct_rate((int) Math.round(rate / 5 * 100));
            pd.setProduct_id("JD_" + this.product_id);
            pd.save_model();
            flag = false;
        }
        try {
            int PageNum = (Integer.parseInt(page.getUrl().regex("page=(\\d+)", 1).toString()));
            JSONObject raw = JSONObject.parseObject(page.getRawText());
            JSONArray result = raw.getJSONArray("comments");

            for (Object item : result) {
                JSONObject oneComment = (JSONObject) item;
                if (oneComment.getString("content").length() <= 4) {
                    continue;
                }

//                    System.out.println(oneComment.getString("rateDate"));
                Comment one = new Comment();
                one.comment_id = "JD_" + oneComment.getString("id");
                one.comment_content = oneComment.getString("content");

                one.comment_time = Utils.String2Date(oneComment.getString("creationTime"), "yyyy-MM-dd HH:mm:ss");
                one.product_id = "JD_" + this.product_id;
                one.save_model();

                JSONArray photos = oneComment.getJSONArray("images");
                if (photos != null && photos.size() > 0) {
                    for (Object item2 : photos) {
                        JSONObject poJSON = (JSONObject) item2;
                        Photo po = new Photo();
                        po.comment_id = one.comment_id;
                        po.photo_url = poJSON.getString("imgUrl").replace("s128x96_jfs", "s616x405_jfs");
                        po.save_model();
                    }

                }

            }

            if (PageNum == 1) {
                int maxPage = raw.getInteger("maxPage");
                if (maxPage > 50) {
                    maxPage = 50;
                }
                for (int i = 2; i <= maxPage; i++) {
                    String nextUrl = page.getUrl().replace("page=\\d+", "page=" + i).toString();
                    page.addTargetRequest(nextUrl);
                }
            }
        } catch (Exception e) {
            mylog.error("", e);

        }

    }

    @Override
    public Site getSite() {
        return site;
    }
}
